<?php

namespace app\api\service;

use think\facade\Db;

class UserCollectGoodsService
{

    /**
     * 获取收藏商品列表
     * */
    public static function getUserCollectGoodsList($user_id)
    {
        $groupGoodsList = Db::name('user_collect_goods')
            ->field('a.goods_id,b.goods_name,b.main_image,b.market_price,b.sell_price,b.is_subsidy,b.is_limit_member,b.tags')
            ->alias('a')
            ->join('goods b','a.goods_id=b.id')
            ->withAttr('main_image',function ($value){
                return request()->domain() . $value;
            })->withAttr('tags',function ($value){
                if (empty($value)){
                    return  [];
                }
                return json_decode($value,true);
            })->where('a.user_id',$user_id)
            ->order('a.id','desc')
            ->paginate(10);

        return $groupGoodsList;
    }

    /**
     * 收藏商品
     * */
    public static function saveUserCollectGoods($user_id,$goods_id)
    {
        $collect = Db::name('user_collect_goods')->where(['user_id' => $user_id,
            'goods_id' => $goods_id
        ])->find();

        if (!empty($collect)) {
            return  $collect;
        }

        $result =  Db::name('user_collect_goods')->insert([
            'user_id' => $user_id,
            'goods_id' => $goods_id
        ]);

        return $result;
    }


    /**
     * 删除收藏商品
     * */
    public static function deleteUserCollectGoods($user_id,$ids)
    {
       
        // $ids = array_column($param,'goods_id');

        $result = Db::name('user_collect_goods')->where('user_id',$user_id)->whereIn('goods_id',$ids)->delete();

        return $result;
    }
}